package org.sanguine.study.hometasks.first.task.a;

import org.sanguine.study.hometasks.first.Runner;

public class TaskA3 extends CommonStringTask {
	
	private int averangeLength;

	public static void main(String[] args) {
		Runner runner = new TaskA3();
		runner.run(args);
	}
	
	@Override
	public void doTask() {
		if (in == null || out == null || numberOfItems <= 0) return;
		readItems();
		if (items != null) countAverange();
		printShorterAverangeLengthResult();
		printLongerAverangeLengthResult();		
	}

	private void printShorterAverangeLengthResult() {
		out.printf("Shorter than averange length (%d):\n", averangeLength);
		for (String s : items)
			if (s.length() < averangeLength) out.printf("%d: '%s'\n", s.length(), s);
	}

	private void printLongerAverangeLengthResult() {
		out.printf("Longer than averange length (%d):\n", averangeLength);
		for (String s : items)
			if (s.length() > averangeLength) out.printf("%d: '%s'\n", s.length(), s);
	}

	private void countAverange() {
		int cumLength = 0;
		for (String s : items) {
			cumLength += s.length();
		}
		averangeLength = cumLength / numberOfItems;
	}
}
